package l2hc.loginserver.network.gameservercon.gspackets;

import l2hc.loginserver.accounts.Account;
import l2hc.loginserver.network.gameservercon.ReceivablePacket;
import l2hc.loginserver.network.gameservercon.lspackets.ChangePasswordResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @date 27.04.12
 * @time 12:54
 */
public class ChangePassword extends ReceivablePacket
{
	public static final Logger _log = LoggerFactory.getLogger(ChangePassword.class);
	private String _account;
	private String _oldPass;
	private String _newPass;
	private String _hwid;

	protected void readImpl()
	{
		_account = readS();
		_oldPass = readS();
		_newPass = readS();
		_hwid = readS();
	}

	protected void runImpl()
	{
		Account acc = new Account(this._account);
		acc.restore();

		if(acc.getPasswordHash() != null)
		{
			if (acc.changePassword(_oldPass, _newPass, _hwid))
			{
				getClient().sendPacket(new ChangePasswordResponse(_account, true));
			}
			else
			{
				getClient().sendPacket(new ChangePasswordResponse(_account, false));
			}

			acc.update();
		}
	}
}
